﻿/*
 
    This file is part of AnkhBG.

    AnkhBG is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AnkhBG is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with AnkhBG.  If not, see <http://www.gnu.org/licenses/>.
 
 */

using System;
using Microsoft.Win32; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Security.Principal;
using System.Xml;
using System.Xml.Serialization;

namespace AnkhBG
{
    public class Settings
    {
        // Warsong Gulch, Arathi Basin, Alterac Valley, Eye of the Storm task file settings!!

        private static Settings _Instance = null;

        #region Properties 

        private string _AVTaskDir = "";
        public string AVTaskDir
        {
            get
            {
                return _AVTaskDir;
            }
            set
            {
                _AVTaskDir = value;
                Save();
            }
        }
        private string _WGTaskDir = "";
        public string WGTaskDir
        {
            get
            {
                return _WGTaskDir;
            }
            set
            {
                _WGTaskDir = value;
                Save();
            }
        }
        private string _ABTaskDir= "";
        public string ABTaskDir
        {
            get
            {
                return _ABTaskDir;
            }
            set
            {
                _ABTaskDir = value;
                Save();
            }
        }
        private string _EoTSTaskDir = "";
        public string EoTSTaskDir
        {
            get
            {
                return _EoTSTaskDir;
            }
            set
            {
                _EoTSTaskDir = value;
                Save();
            }
        }
        private string _UseProfiles = "";
        public string UseProfiles
        {
            get
            {
                return _UseProfiles;
            }
            set
            {
                _UseProfiles = value;
                Save();
            }
        }
        private string _QueueType = "";
        public string QueueType
        {
            get { return _QueueType; }
            set
            {
                _QueueType = value;
                Save();
            }
        }
        private string _QueueBG = "";
        public string QueueBG
        { 
            get { return _QueueBG; }
            set
            {
                _QueueBG = value;
                Save();
            }
        }
        private string _PatherDeaths = "";
        public string PatherDeaths
        {
            get { return _PatherDeaths; }
            set
            {
                _PatherDeaths = value;
                Save();
            }
        }
        private string _JoinAfter = "-1";
        public string JoinAfter
        {
            get { return _JoinAfter; }
            set
            {
                _JoinAfter = value;
                Save();
            }
        }
        private string _RandomTitles = "false";
        public string RandomTitles
        {
            get { return _RandomTitles; }
            set
            {
                if (value != _RandomTitles)
                {
                    _RandomTitles = value;
                    Save();

                    if (Main.__Instance != null)
                    {
                        if (value == "true")
                        {
                            if (Main.Instance.Window != null)
                                Main.Instance.Window.Text = Glider.Common.Objects.GContext.Main.GetRandomString();
                            if (Main.Instance.Config != null && Main.Instance.Config.Window != null)
                                Main.Instance.Config.Window.Text = Glider.Common.Objects.GContext.Main.GetRandomString();
                            if (Main.Instance.Stats != null && Main.Instance.Stats.Window != null)
                                Main.Instance.Stats.Window.Text = Glider.Common.Objects.GContext.Main.GetRandomString();
                        }
                        else
                        { // set back to normal titles
                            if (Main.Instance.Window != null)
                                Main.Instance.Window.Text = "AnkhBG";
                            if (Main.Instance.Config != null && Main.Instance.Config.Window != null)
                                Main.Instance.Config.Window.Text = "Configuration";
                            if (Main.Instance.Stats != null && Main.Instance.Stats.Window != null)
                                Main.Instance.Stats.Window.Text = "Settings";
                        }
                    }
                }
            }
        }

        private String _GliderTimer = "-1";
        public String GliderTimer {
            get { return _GliderTimer; }
            set { _GliderTimer = value; Save(); } 
        }

        private String _BeginDelay = "-1";
        public String BeginDelay
        {
            get { return _BeginDelay; }
            set { _BeginDelay = value; Save(); }
        }

        #endregion 

        #region load/save
        public static string SettingsFile
        {
            get
            {
                return @"AnkhBG.xml";
            }
        }

        public void Save()
        {
            try
            {
                Environment.CurrentDirectory = Main.path;

                Stream stream = File.Create(SettingsFile);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(stream, this);
                stream.Close();
            }
            catch
            {
            }
        }

        public void MakeDefault()
        {
            AVTaskDir = "";
            WGTaskDir = "";
            ABTaskDir = "";
            EoTSTaskDir = "";
            UseProfiles = "";
            QueueType = "";
            QueueBG = "";
            PatherDeaths = "";
            JoinAfter = "-1";
            GliderTimer = "-1";
            RandomTitles = "false";
            Save();
        }
        #endregion

        #region Singleton
        private Settings()
        {
        }

        public static Settings Instance
        {
            get
            {
                if (_Instance == null)
                {
                    try
                    {
                        Stream stream = File.OpenRead(SettingsFile);
                        XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                        _Instance = (Settings)serializer.Deserialize(stream);
                    }
                    catch
                    {
                        _Instance = new Settings();
                    }
                }

                return _Instance;
            }
        }
        #endregion
    }
}
